package com.julis.activity.setting.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.github.czy1121.view.RoundButton;
import com.julis.R;
import com.julis.activity.record.adapter.MyRVUserAdapter;
import com.julis.bean.Life_UserInfo;
import com.julis.db.Z_UserInfoDao;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 用户管理
 */
public class User_Activity extends AppCompatActivity {

    MyRVUserAdapter mAdapter;
    Z_UserInfoDao z_obuInfoDao;
    List<Life_UserInfo> obu_infos;

    @BindView(R.id.recyclerView)
    RecyclerView mRvMain;
    @BindView(R.id.bt_chaxun)
    RoundButton btChaxun;
    @BindView(R.id.bt_daochu)
    RoundButton btDaochu;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        ButterKnife.bind(this);
        z_obuInfoDao = new Z_UserInfoDao(this);
        init();
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e("zzz", "启动");
        shuaxin();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("zzz", "重新启动");
    }

    public void init() {


        // 设置布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRvMain.setLayoutManager(linearLayoutManager);
        mRvMain.addItemDecoration(
                new HorizontalDividerItemDecoration.Builder(this).build());
        mAdapter = new MyRVUserAdapter(this, z_obuInfoDao);
        mAdapter.addfragment(this);
        mRvMain.setAdapter(mAdapter);

        shuaxin();
        mAdapter.setOnItemClickListener(new MyRVUserAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
//                Toast.makeText(UIUtil.getContext(), "click" + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
//                Toast.makeText(UIUtil.getContext(), "long click" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void shuaxin() {
        mAdapter.clear();
        obu_infos = z_obuInfoDao.queryForAll();
        for (int i = 0; i < obu_infos.size(); i++) {
            if (obu_infos.get(i).getAccount().equals("admin")) {
                obu_infos.remove(obu_infos.get(i));
            }

        }
        if (obu_infos != null && obu_infos.size() > 0) {
            Collections.reverse(obu_infos);
        }
        if (obu_infos != null && obu_infos.size() > 0) {
            mAdapter.addItem(obu_infos);
        }
        mAdapter.notifyDataSetChanged();
    }


    @OnClick({R.id.img_back,   R.id.bt_chaxun})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.bt_chaxun:
                startActivity(new Intent(this, UserAdd_Activity.class));
                break;


        }
    }


    @OnClick(R.id.img_back)
    public void onViewClicked() {
    }
}
